package com.goodving.guli.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.goodving.guli.R;
import com.goodving.guli.bt.C0668a;
import com.goodving.guli.bt.C1468p;
import com.goodving.guli.bt.Utils;
import com.goodving.guli.bt.eventbus.EventPersonInfoUpdate;
import com.goodving.guli.bt.model.DataManager;
import com.goodving.guli.bt.model.PersonInfo;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.bt.utils.WeightUtil;
import com.goodving.guli.bt.weight.family.WeightFamilyMemberActivity;
import com.goodving.guli.model.Keeper;
import com.goodving.guli.model.UserTotalSportData;

import cn.com.smartdevices.bracelet.chart.util.ChartData;
import cn.com.smartdevices.bracelet.config.Config;
import de.greenrobot.event.EventBus;

/**
 * Created by john on 2015/12/24.
 */
public class MyPageActivity extends SystemBarTintActivity implements View.OnClickListener {
    private static final String TAG = "MyPageActiv";
    private static final int f9083k = 20;
    private PersonInfo mPersonInfo;
    private TextView totalDistance;
    private Config f9086d;
    private TextView stepGoal;
    private TextView weightGoal;
    private ImageView avater;
    private Context mContext;
    private TextView nickName;
    private View menuFamily;

    public MyPageActivity() {
        this.mContext = this;
    }

    private void m13962a() {
        Utils.m8007a(this.mPersonInfo, this.avater);
        this.nickName.setText(this.mPersonInfo.getNickname());
        this.stepGoal.setText(this.mPersonInfo.getDaySportGoalSteps() + getString(R.string.step_per_day));
        GuliLog.tracePrintInfo(TAG, "mPersonInfo.targetWeight " + this.mPersonInfo.targetWeight + " after change " + WeightUtil.m15706b(this.mPersonInfo.targetWeight, this.mPersonInfo.getWeightUnit()));
        if (Config.m8876h().f4202l.f4259a.booleanValue()) {
            if (this.mPersonInfo.targetWeight > 0.0f) {
                this.weightGoal.setText(WeightUtil.m15716c(WeightUtil.m15706b(this.mPersonInfo.targetWeight, this.mPersonInfo.getWeightUnit()), 1) + WeightUtil.m15698a((Context) this, this.mPersonInfo.miliConfig.weightUnit));
            } else {
                this.weightGoal.setText(R.string.not_set);
            }
        }
        m13967d();
    }

    private void m13963a(Context context, Class<?> cls, String str) {
        Intent intent = new Intent();
        intent.setClass(context, cls);
        context.startActivity(intent);
        C0668a.m8128a((Context) this, C0668a.dg, str);
    }

    private void m13964b() {
        findViewById(R.id.my_page_person_info).setOnClickListener(this);
        this.avater = (ImageView) findViewById(R.id.my_page_avater);
        this.nickName = (TextView) findViewById(R.id.my_page_person_name);
        this.totalDistance = (TextView) findViewById(R.id.my_page_person_total_dist);
        this.stepGoal = (TextView) findViewById(R.id.device_set_goal_text);
        findViewById(R.id.device_set_goal).setOnClickListener(this);
        View findViewById = findViewById(R.id.info_weight_goal);
        View findViewById2 = findViewById(R.id.info_weight_goal_split);
        View findViewById3 = findViewById(R.id.info_weight_goal_split);
        this.menuFamily = findViewById(R.id.menu_family);
//        if (Config.m8876h().f4202l.f4259a.booleanValue()) {
            findViewById.setVisibility(View.VISIBLE);
            findViewById2.setVisibility(View.VISIBLE);
            findViewById3.setVisibility(View.VISIBLE);
            this.weightGoal = (TextView) findViewById(R.id.info_weight_goal_value);
            findViewById(R.id.info_weight_goal).setOnClickListener(this);
//        } else {
//            findViewById.setVisibility(View.GONE);
//            findViewById2.setVisibility(View.GONE);
//            this.menuFamily.setVisibility(View.GONE);
//            findViewById3.setVisibility(View.GONE);
//        }
        findViewById(R.id.info_unit_set).setOnClickListener(this);
        findViewById(R.id.my_page_about_area).setOnClickListener(this);
        this.menuFamily.setOnClickListener(this);
    }

    class cD implements C1468p {
        final /* synthetic */ MyPageActivity activity;

        cD(MyPageActivity myPageActivity) {
            this.activity = myPageActivity;
        }

        public void m12295a (UserTotalSportData userTotalSportData) { // m14531a
            GuliLog.tracePrintInfo("MyPageActiv", "returnSportData");
            this.activity.mPersonInfo.totalSportData = userTotalSportData;
            Keeper.keepPersonInfo(this.activity.mPersonInfo);
            this.activity.m13967d();
        }
    }

    private void m13966c() {
        DataManager.m12179a().m12205a(new cD(this));
    }

    private void m13967d() {
        String[] a = ChartData.m8737a((Context) this, this.mPersonInfo.getTotalDistance(), 1);
        if (this.mPersonInfo.getTotalDistance() > 0) {
            this.totalDistance.setText(getString(R.string.total_distance_format, new Object[]{a[0] + a[1]}));
            return;
        }
        this.totalDistance.setText("--");
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        if (f9083k != i) {
            return;
        }
        if (WeightGoalSetActivity.f11180c == i2) {
            EventBus.getDefault().post(new EventPersonInfoUpdate(null));
        } else if (WeightGoalSetActivity.f11181d == i2) {
            float floatExtra = intent.getFloatExtra(WeightGoalSetActivity.f11179b, 0.0f);
            this.mPersonInfo.targetWeight = WeightUtil.m15689a(floatExtra, Keeper.readPersonInfo().miliConfig.weightUnit);
            EventBus.getDefault().post(new EventPersonInfoUpdate(null));
        }
    }

    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.device_set_goal:
                intent.setClass(this, HealthGoalsActivity.class);
                break;
            case R.id.my_page_person_info:
                intent.setClass(this, PersonInfoActivity.class);
                break;
            case R.id.menu_family:
                intent.setClass(this, WeightFamilyMemberActivity.class);
                C0668a.m8126a(this.mContext, C0668a.ge);
                break;
            case R.id.info_weight_goal:
                intent.setClass(this.mContext, WeightGoalSetActivity.class);
                intent.putExtra(WeightGoalSetActivity.f11178a, -1);
                startActivityForResult(intent, f9083k);
                return;
            case R.id.info_unit_set:
                intent.setClass(this, PersonInfoSettingUnitActivity.class);
                break;
            case R.id.my_page_about_area:
                intent.setClass(this, SettingAboutActivity.class);
                break;
        }
        startActivity(intent);
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView((int) R.layout.my_page_activity);
        this.f9086d = Config.m8876h();
        this.mPersonInfo = Keeper.readPersonInfo();
        m13964b();
        m13966c();
    }



    protected void onResume() {
        super.onResume();
        this.mPersonInfo = Keeper.readPersonInfo();
        m13962a();
    }
}
